import React, { forwardRef, useImperativeHandle } from 'react';
import { Link } from 'react-router-dom';
import { Table, Space, Button, Modal, message } from 'antd';
import { Authorized } from '@/components';
import permission from '@/config/permission';
import enums from '@/enums';
import { useTaskList, useDownloadResourceFile, useDeleteTask } from './hooks';

/**
 * 导入任务列表
 */
function TaskList(props, ref) {
    const { form } = props;

    const [{ confirm }, contextHolder] = Modal.useModal();

    const taskList = useTaskList({ form });

    const downloadResourceFile = useDownloadResourceFile({
        onSuccess() {
            message.success('下载成功');
        },
        onError(err) {
            message.error('下载失败');
        },
    });

    const deleteTask = useDeleteTask({
        onSuccess() {
            taskList.refresh();
            message.success('删除成功');
        },
        onError() {
            message.error('删除失败');
        },
    });

    useImperativeHandle(ref, () => taskList);

    return (
        <>
            <Table
                rowKey="ossFileUploadTaskId"
                scroll={{ x: 'max-content' }}
                columns={[
                    {
                        title: '导入任务名称/ID',
                        dataIndex: 'ossFileUploadTaskName',
                        render(text, record) {
                            return (
                                <>
                                    <Link to={{ pathname: '/resource/import-tasks/detail', state: record }}>
                                        {text}
                                    </Link>
                                    <div>{record.ossFileUploadTaskId}</div>
                                </>
                            );
                        },
                    },
                    {
                        title: '云区域名称',
                        dataIndex: 'cloudRegionName',
                    },
                    {
                        title: '状态',
                        dataIndex: 'status',
                        render: (value = '') => (
                            <span style={{ color: value.includes('fail') ? '#D9001B' : undefined }}>
                                {enums.ossImport.taskStatus.get(value) ?? '-'}
                            </span>
                        ),
                    },
                    {
                        title: '导入任务类型',
                        dataIndex: 'ossFileUploadType',
                        render: (value) => enums.ossImport.taskTypes.get(value),
                    },
                    {
                        title: '操作人',
                        dataIndex: 'author',
                        render: (text) => text ?? '-',
                    },
                    {
                        title: '操作时间',
                        dataIndex: 'createTime',
                    },
                    {
                        title: '操作',
                        render({ ossFileUploadTaskId, status, ossFileUploadTaskName }) {
                            return (
                                <Space size="middle">
                                    <Authorized authority={permission.resourceImportTasks_download} noMatch={null}>
                                        <Button
                                            style={{ padding: 0 }}
                                            type="link"
                                            disabled={
                                                ![
                                                    'upload-success',
                                                    'check-ing',
                                                    'check-success',
                                                    'check-fail',
                                                ].includes(status)
                                            }
                                            onClick={() => {
                                                downloadResourceFile.run(ossFileUploadTaskId);
                                            }}
                                        >
                                            下载
                                        </Button>
                                    </Authorized>
                                    <Authorized authority={permission.resourceImportTasks_delete} noMatch={null}>
                                        <Button
                                            style={{ padding: 0 }}
                                            type="link"
                                            disabled={!['upload-fail', 'check-success', 'check-fail'].includes(status)}
                                            onClick={() => {
                                                confirm({
                                                    title: '删除导入任务',
                                                    content: `确认删除导入任务“${ossFileUploadTaskName}”？`,
                                                    onOk() {
                                                        return deleteTask.runAsync(ossFileUploadTaskId);
                                                    },
                                                });
                                            }}
                                        >
                                            删除
                                        </Button>
                                    </Authorized>
                                </Space>
                            );
                        },
                    },
                ]}
                {...taskList.tableProps}
                pagination={{
                    showSizeChanger: true,
                    showTotal: (total) => `共 ${total} 条`,
                    ...taskList.tableProps.pagination,
                }}
            />
            {contextHolder}
        </>
    );
}

export default forwardRef(TaskList);
